const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowW: '',
    windowH: '',
    items: '', //我的数据
    register: false,
    show: false,
    jinqun: '', //进群参数
    num: '', //反馈数
    phone: false,
    vip: '', //是否为会员
    domain: Api.domain,
    domian: app.globalData.domian,
    shua: {
      statistics: '', ////本周刷体谅
    } 
  },

  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowW: res.windowWidth,
          windowH: res.windowHeight
        })
      }
    })
    // this.jinqun()
    this.shuati()
  },
  hong() { //红点数
    var that = this
    var params = {
      member_id: network.get('member_id')
    }
    network.requestLoading(Api.GET.index_feedback, params, 3, '', 'GET', '', function (res) { //成功的方法
      console.log('红点数', res)
      that.setData({
        num: res
      })
    }, function () {})
  },
  shuati() { //刷题统计
    wx.$http.get(wx.$get.subject_statistics_log, {}).then(res => {
      console.log(res, 'ben刷题量')
      this.setData({
        'shua.statistics': res
      })
      for (let i = 0; i <= 100; i++) {
        setTimeout(() => {
          this.drawCanvas(0.01 * i)
        }, 200 - 100 / i);
      }
    })
  },
  init() { //我的
    var that = this
    var params = {

    }
    wx.$http.get(wx.$get.get_member_info, params).then(res => {
      if(!res.vip_time){
        res.vip_time='1970-01-01'
      }
      console.log(res, '用户信息')
      wx.$cache.set('userinfo', res)
      wx.$cache.set('member_id', res.id)
      this.setData({
        items: res,
        vip: this.judgeTime(res.vip_time)
      })
    })
  },
  drawCanvas(length) { //开始画canvas
    // console.log('开始画canvas')
    var that = this
    var ctx = wx.createCanvasContext('mycanvas')
    var windowW = that.data.windowW
    var rpx = windowW / 750;
    ctx.clearRect(0, 0, 600 * rpx, 430 * rpx)
    var windowH = that.data.windowH
    var week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
    var ti = []
    let items = this.data.shua
    var ti2 = [items.statistics[0].num * length, items.statistics[1].num * length, items.statistics[2].num * length, items.statistics[3].num * length, items.statistics[4].num, items.statistics[5].num * length, items.statistics[6].num * length, ]
    var ti3 = [items.statistics[0].num, items.statistics[1].num, items.statistics[2].num, items.statistics[3].num, items.statistics[4].num, items.statistics[5].num, items.statistics[6].num, ]
    // console.log(Math.max(...ti2))
    let maxshu = Math.max(...ti3)
    if (maxshu >= 3) {
      let zheng = Math.ceil(maxshu / 3)
      // console.log(zheng)
      ti[3] = 0
      ti[2] = zheng
      ti[1] = 2 * zheng
      ti[0] = 3 * zheng
    } else {
      ti = ['3', '2', '1', '0']
    }
    ctx.setFillStyle('#918E82')
    ctx.setFontSize(26 * rpx)
    ctx.fillText(ti[0], 0 * rpx, 42 * rpx)
    ctx.fillText(ti[1], 0 * rpx, 142 * rpx)
    ctx.fillText(ti[2], 0 * rpx, 237 * rpx)
    ctx.fillText(ti[3], 0 * rpx, 335 * rpx)
    this.xu(ctx, 38 * rpx, 30 * rpx, 583 * rpx, 30 * rpx)
    this.xu(ctx, 38 * rpx, 132 * rpx, 583 * rpx, 132 * rpx)
    this.xu(ctx, 38 * rpx, 230 * rpx, 583 * rpx, 230 * rpx)
    this.xu(ctx, 38 * rpx, 330 * rpx, 583 * rpx, 330 * rpx)
    ctx.setFontSize(24 * rpx)
    ctx.fillText(week[0], 50 * rpx, 377 * rpx)
    ctx.fillText(week[1], 140 * rpx, 377 * rpx)
    ctx.fillText(week[2], 220 * rpx, 377 * rpx)
    ctx.fillText(week[3], 307 * rpx, 377 * rpx)
    ctx.fillText(week[4], 394 * rpx, 377 * rpx)
    ctx.fillText(week[5], 471 * rpx, 377 * rpx)
    ctx.fillText(week[6], 551 * rpx, 377 * rpx)
    this.xian(ctx, ti, ti2, rpx)
    this.yuan(ctx, 65 * rpx, this.height(ti[0], ti2, 0) * rpx, 7 * rpx)
    this.yuan(ctx, 155 * rpx, this.height(ti[0], ti2, 1) * rpx, 7 * rpx)
    this.yuan(ctx, 236 * rpx, this.height(ti[0], ti2, 2) * rpx, 7 * rpx)
    this.yuan(ctx, 320 * rpx, this.height(ti[0], ti2, 3) * rpx, 7 * rpx)
    this.yuan(ctx, 410 * rpx, this.height(ti[0], ti2, 4) * rpx, 7 * rpx)
    this.yuan(ctx, 488 * rpx, this.height(ti[0], ti2, 5) * rpx, 7 * rpx)
    this.yuan(ctx, 565 * rpx, this.height(ti[0], ti2, 6) * rpx, 7 * rpx)

    ctx.draw()
  },
  xu(ctx, a, b, c, d) { //虚线
    ctx.setLineDash([5, 3], 1);
    ctx.beginPath();
    ctx.setStrokeStyle('#F1F1F1')
    ctx.moveTo(a, b);
    ctx.lineTo(c, d);
    ctx.stroke();
  },
  height(max, arr2, index) { //圆圈所在的高度
    return 300 - 300 / max * arr2[index] + 30
  },
  xian(ctx, ti, ti2, rpx) { //所连接起来的线
    ctx.beginPath()
    var grd = ctx.createLinearGradient(0 * rpx, 0 * rpx, 0 * rpx, 432 * rpx);
    ctx.setLineDash([0, 0], 0);
    grd.addColorStop(0, '#FDD655')
    grd.addColorStop(1, '#FFFAE8')
    ctx.setStrokeStyle('#FDD655')
    ctx.setFillStyle(grd)
    ctx.moveTo(65 * rpx, 329 * rpx)
    ctx.lineTo(65 * rpx, this.height(ti[0], ti2, 0) * rpx)
    ctx.lineTo(155 * rpx, this.height(ti[0], ti2, 1) * rpx)
    ctx.lineTo(236 * rpx, this.height(ti[0], ti2, 2) * rpx)
    ctx.lineTo(320 * rpx, this.height(ti[0], ti2, 3) * rpx)
    ctx.lineTo(410 * rpx, this.height(ti[0], ti2, 4) * rpx)
    ctx.lineTo(488 * rpx, this.height(ti[0], ti2, 5) * rpx)
    ctx.lineTo(565 * rpx, this.height(ti[0], ti2, 6) * rpx)
    ctx.lineTo(565 * rpx, 329 * rpx)
    ctx.closePath()
    ctx.stroke();
    ctx.fill()
    ctx.beginPath()
    ctx.setStrokeStyle('#fff')
    ctx.moveTo(565 * rpx, this.height(ti[0], ti2, 6) * rpx)
    ctx.lineTo(565 * rpx, 329 * rpx)
    ctx.lineTo(65 * rpx, 329 * rpx)
    ctx.lineTo(65 * rpx, this.height(ti[0], ti2, 0) * rpx)
    ctx.stroke();
    ctx.closePath()
  },
  yuan(ctx, a, b, c, d) { //圆
    ctx.beginPath()
    ctx.arc(a, b, c, 0, 2 * Math.PI)
    ctx.setFillStyle('#FDD655')
    ctx.fill()
  },
  jinqun() { //进群参数
    var that = this
    var params = {

    }
    network.requestLoading(Api.GET.reply_qun, params, 3, '', 'GET', '', function (res) { //成功的方法
      console.log('进群参数', res)
      that.setData({
        jinqun: res
      })
    }, function () {})
  },
  gohui2(){
    wx.navigateTo({
      url: '/pages/memberCenter/memberCenter',
    })
  },
  gohui() { //去会员中心
    if (this.data.vip) {
      // wx.navigateTo({
      //   url: '/pages/entergroup/entergroup',
      // })
      this.setData({
        show: true
      })
    } else {
      wx.navigateTo({
        url: '/pages/memberCenter/memberCenter',
      })
    }
  },
  close() { //取消弹框
    this.setData({
      show: false
    })
  },
  gofeek() { //跳转报错反馈列表
    wx.navigateTo({
      url: '/pages/feedbackList/feedbackList',
    })
  },


  judgeTime(time) { //判断时间是否过期
    if (!time) {
      return false
    }
    var strtime = time.replace(/-/g, "/"); //时间转换
    //时间

    var date1 = Date.parse(new Date(strtime));
    //现在时间
    var date2 = Date.parse(new Date());

    //判断时间是否过期
    return date1 < date2 ? false : true;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
     
      ios:app.globalData.ios,
    
    })
    this.init()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: wx.$cache.get('fx').title,
      imageUrl: wx.$cache.get('fx').img,
      path: `/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})